{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e5c8078f",
   "metadata": {},
   "source": [
    "__________________________  \n",
    "* 时间：2022/10/17 week09  \n",
    "* 记录人： 江柯廷  \n",
    "__________________________"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8212cb30",
   "metadata": {},
   "source": [
    "# 本周知识点  \n",
    "* 1. 用户输入  \n",
    "* 2. 文本格式化输出  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "082832e7",
   "metadata": {},
   "source": [
    "## 1. 用户输入  \n",
    "* input方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a9a7d805",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：admin\n",
      "密码：123456\n"
     ]
    }
   ],
   "source": [
    "# 用户登录系统  \n",
    "uesrname = input(\"账号：\")  \n",
    "password = input(\"密码：\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "969ff835",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "admin\n",
      "123456\n"
     ]
    }
   ],
   "source": [
    "print(uesrname)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "49c895bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：njhdf1\n",
      "密码：4819!\n",
      "账号不存在或密码不正确！请重新输入！\n"
     ]
    }
   ],
   "source": [
    "# 用户登录系统 迭代1\n",
    "user01 = \"admin\"\n",
    "password01 = \"123456\"\n",
    "\n",
    "\n",
    "username = input(\"账号：\")\n",
    "password = input(\"密码：\")\n",
    "# 情景一： 账号密码都正确\n",
    "if user01 == username and password01 == password:\n",
    "    print(\"登录成功~欢迎\"+user01)\n",
    "# 情景二：账号不存在或密码不存在\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确！请重新输入！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1a58a2f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：admin\n",
      "密码：123456\n",
      "登录成功~欢迎admin\n"
     ]
    }
   ],
   "source": [
    "# 用户登录系统 迭代2 循环！ 5分钟，加油~for + range()\n",
    "\n",
    "user01 = \"admin\"\n",
    "password01 = \"123456\"\n",
    "\n",
    "for i in range(3,0,-1):\n",
    "    username = input(\"账号：\")\n",
    "    password = input(\"密码：\")\n",
    "    #情景一：账号密码都正确\n",
    "\n",
    "    #情景二：账号不存在或密码不正确，用户可以输入三次，三次之后1小时之后再来尝试\n",
    "    if user01 == username and password01 == password:\n",
    "        print(\"登录成功~欢迎\"+user01)\n",
    "        break # 中止循环\n",
    "    else:\n",
    "        if i>1:\n",
    "             print(\"账号不存在或密码不正确！请重新输入！您还有\"+str(i-1)+\"次尝试机会\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确！请一小时后再来尝试\")\n",
    "                "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71a8a1a7",
   "metadata": {},
   "source": [
    "## 2.字符串的格式化输出  \n",
    "\n",
    "> 1. 字符串的拼接  \n",
    "> 2. 占位符  \n",
    "> 3. 格式化输出的方法：format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3bb3b3f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "昵称：李华\n",
      "地区:深圳\n",
      "工作：产品经理助理\n",
      "年龄：22\n",
      "期待薪资：10k/月\n"
     ]
    }
   ],
   "source": [
    "# 用户的信息输入(完善信息)  \n",
    "name = input(\"昵称：\")  \n",
    "region = input(\"地区:\")\n",
    "job = input(\"工作：\")\n",
    "age = input(\"年龄：\")\n",
    "salary = input(\"期待薪资：\")\n",
    "\n",
    "info = '''----------INFO'''+ name + '''-----------\n",
    "地区： '''+ region + '''\n",
    "工作: '''+ job + '''\n",
    "年龄: '''+ age + '''\n",
    "薪资期待: '''+salary\n",
    "# 好麻烦，好容易出错！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f65b6a38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------INFO李华-----------\n",
      "地区： 深圳\n",
      "工作: 产品经理助理\n",
      "年龄: 22\n",
      "薪资期待: 10k/月\n"
     ]
    }
   ],
   "source": [
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "862db0c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "昵称：江少\n",
      "地区:重庆或广州\n",
      "工作：CEO\n",
      "年龄：20\n",
      "期待薪资：100W/月\n",
      "---------INFO 江少 --------\n",
      "地区： 重庆或广州\n",
      "工作： CEO\n",
      "年龄： 20\n",
      "薪资期待： 100W/月\n"
     ]
    }
   ],
   "source": [
    "# 用户的信息输入(完善信息) ：占位符 s%\n",
    "name = input(\"昵称：\")  \n",
    "region = input(\"地区:\")\n",
    "job = input(\"工作：\")\n",
    "age = input(\"年龄：\")\n",
    "salary = input(\"期待薪资：\")\n",
    "\n",
    "info = '''---------INFO %s --------\n",
    "地区： %s\n",
    "工作： %s\n",
    "年龄： %s\n",
    "薪资期待： %s'''%(name,region,job,age,salary)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fef4fb03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "昵称：江少\n",
      "地区:深圳\n",
      "工作：CEOpython\n",
      "年龄：20\n",
      "期待薪资：120W/月\n",
      "---------INFO 江少 --------\n",
      "地区： 深圳\n",
      "工作： CEOpython\n",
      "年龄： 20\n",
      "薪资期待： 120W/月\n"
     ]
    }
   ],
   "source": [
    "# 用户的信息输入(完善信息) ：format() # 不考虑顺序，只考虑一一对应的关系\n",
    "name = input(\"昵称：\")  \n",
    "region = input(\"地区:\")\n",
    "job = input(\"工作：\")\n",
    "age = input(\"年龄：\")\n",
    "salary = input(\"期待薪资：\")\n",
    "\n",
    "info = '''---------INFO {昵称} --------\n",
    "地区： {地区}\n",
    "工作： {工作职位}\n",
    "年龄： {年龄}\n",
    "薪资期待： {薪资}'''.format(工作职位=job,薪资=salary,昵称=name,年龄=age,地区=region)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0401950",
   "metadata": {},
   "source": [
    "## 课后作业：  \n",
    "> 1. 第二章p93 前所有项目练习(下周讲)  \n",
    "> 2. 购物车项目  \n",
    ">> 要求：  \n",
    ">> 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户  \n",
    ">> 2. 让用户输入商品编号进行商品的购买  \n",
    ">> 3. 用户选择商品后。检查用户余额是否够，够则直接扣款，不够则提醒用户  \n",
    ">> 4. 用户可随时退出购买，退出时打印用户已购买的商品和支付宝余额  \n",
    "* 温馨提示，网上有许多购物车项目，大多为了“炫技”，但百分之99以上看不懂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d6147f38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入预算：5000\n",
      "1     iphone8              5888      \n",
      "2     kndle                988       \n",
      "3     ps4 pro              2800      \n",
      "4     psv                  1200      \n",
      "5     psvtv                450       \n",
      "6     ps4 controler        230       \n",
      "7     mp3                  100       \n",
      "请输入要加入购物车的商品编号：1\n",
      "1 iphone8 5888 * 1\n",
      "总计： 5888 余额： -888\n",
      "---------------------------------\n",
      "1：继续选购 2:整理购物车 3：结算\n",
      "\n",
      "请选择：3\n",
      "余额不足，还差金额： 888\n"
     ]
    }
   ],
   "source": [
    "money = int(input(\"请输入预算：\"))\n",
    "shop = [['1','iphone8',5888],['2','小米9青春版',988],['3','苹果',13],['4','梨',4],\n",
    "        ['5','psvtv',450],['6','ps4',230],['7','mp3',100]]\n",
    "i = 1#为了通过修改i 退出多重循环\n",
    "allChoice = []\n",
    "while(i):\n",
    "    for num in range(len(shop)):  # 打印商品列表\n",
    "        print(str(shop[num][0]).ljust(5), shop[num][1].ljust(20), str(shop[num][2]).ljust(10), )\n",
    "    choice = input(\"请输入要加入购物车的商品编号：\")\n",
    "    choice = [int(it) for it in choice.split(' ')]\n",
    "    allChoice += choice #choice是单次选择的商品列表，allchoice是所有选择的商品列表\n",
    "    while(1):\n",
    "        total = 0\n",
    "        choiceSet = set(allChoice)#转换成集合，便于不重复计数\n",
    "        for it in choiceSet:\n",
    "            print(shop[it-1][0],shop[it-1][1],shop[it-1][2],'*',allChoice.count(it))\n",
    "            total += shop[it-1][2]*allChoice.count(it)\n",
    "        print(\"总计：\",total,\"余额：\",money-total)\n",
    "        print(\"---------------------------------\\n\"\n",
    "              \"1：继续选购 2:整理购物车 3：结算\\n\")\n",
    "        option = int(input( \"请选择：\"))\n",
    "        if option == 1:\n",
    "            break\n",
    "        elif option == 2:\n",
    "            item_num = int(input(\"请输入要删除的商品\"))\n",
    "            allChoice.remove(item_num)#每次只会删除一个元素\n",
    "            continue\n",
    "        else:\n",
    "            if money>=total:\n",
    "                print(\"购物结束，余额为：\",money-total)\n",
    "            else:\n",
    "                print(\"余额不足，还差金额：\",total-money)\n",
    "            i = 0\n",
    "            break#整个退出"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
